//
//  OAuthViewController.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/25.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit
import SVProgressHUD

class OAuthViewController: UIViewController {
    
    private lazy var webView = UIWebView()
    
    //MARK: - 监听方法
    @objc private func close() {
        SVProgressHUD.dismiss()
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func autoFill() {
//        let js = "document.getElementById('userId').value='daoge10000@sina.cn';" + "document.getElementById('passwd').value='qqq123';"
        let js = "document.getElementById('userId').value='13317596550';" + "document.getElementById('passwd').value='smh19931124';"
        webView.stringByEvaluatingJavaScriptFromString(js)
    }
        
    //MARK: - 设置界面
    override func loadView() {
        view = webView
        view.backgroundColor = UIColor.whiteColor()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "autoFill")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        webView.delegate = self
        title = "登陆新浪微博"
        
        webView.loadRequest(NSURLRequest(URL: NetworkTools.sharedNetworkTools.oauthURL))
    }
    
    deinit {
        print("webView 销毁了")
    }
}
//MARK: - UIWebViewDelegate
extension OAuthViewController: UIWebViewDelegate {
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        guard let url = request.URL where url.host == "www.baidu.com" else {
            return true
        }
        guard let quary = url.query where quary.hasPrefix("code=") else {
            print("取消授权")
            self.close()
            return false
        }
        let code = quary.substringFromIndex("code=".endIndex)
        print("授权是 " + code)
        
        UserAccountViewModel.sharedUserAccount.loadAccountToken(code) { (isSuccessed) -> () in
            if !isSuccessed {
                print("失败了")
                SVProgressHUD.showInfoWithStatus("您的网络不给力")
                delay(1, finishedCall: { () -> () in
                    self.close()
                })
                
                return
            }
            print("登陆成功")
            
            self.dismissViewControllerAnimated(false, completion: { () -> Void in
                SVProgressHUD.dismiss()
                
                NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: "welcome")
            })
            
        }
        
        return false
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
}